//
// Created by lz on 2021/2/17.
//

#ifndef MYWEBLIB_NET_ENDIAN_H_
#define MYWEBLIB_NET_ENDIAN_H_

/*
 * 该头文件是对字节序库的简单封装
 *
 */

#include <stdint.h>
#include <endian.h>

namespace lzweb
{
	namespace net
	{
		namespace sockets
		{
			// the inline assembler code makes type blur,    blur->模糊
			// so we disable warnings for a while.

			//在GCC下，#pragma GCC diagnostic push用于记录当前的诊断状态，
			// #pragma GCC diagnostic pop用于恢复诊断状态。
			//可以用于屏蔽局部代码的警告
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wold-style-cast"


			//网络字节序是大端的

			inline uint64_t hostToNetwork64(uint64_t host64)
			{
				return htobe64(host64);
			}
			inline uint32_t hostToNetwork32(uint32_t host32)
			{
				return htobe32(host32);
			}

			inline uint16_t hostToNetwork16(uint16_t host16)
			{
				return htobe16(host16);
			}

			inline uint64_t networkToHost64(uint64_t net64)
			{
				return be64toh(net64);
			}

			inline uint32_t networkToHost32(uint32_t net32)
			{
				return be32toh(net32);
			}

			inline uint16_t networkToHost16(uint16_t net16)
			{
				return be16toh(net16);
			}

#pragma GCC diagnostic pop

		} // namespace sockets
	} // namespace net

}// namespace lzweb


#endif //MYWEBLIB_NET_ENDIAN_H_
